<?php

namespace app\admin\controller;

use think\Controller;

/**
 * 栏目管理
 * Class Cate
 * @package app\admin\controller
 */
class Category extends Common
{
    protected $db;

    /**
     *
     */
    protected function _initialize()
    {
        parent::_initialize();
        $this->db = new \app\common\model\Category();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        //获取栏目数据
        //$field=db('Cate')->select();
        $field = $this->db->getAll();

        $this->assign('field', $field);
        return $this->fetch();
    }

    //添加栏目
    public function store()
    {
        if (request()->isPost()) {
            $res = $this->db->store(input('post.'));
            if ($res['valid']) {
                //执行成功
                $this->success($res['msg'], 'category/index');
                exit;
            } else {
                //执行失败
                $this->error($res['msg']);
                exit;
            }
        }
        return $this->fetch();
    }

    //添加子集栏目
    public function addSon()
    {
        if (request()->isPost()) {
            $res = $this->db->store(input('post.'));
            if ($res['valid']) {
                //执行成功
                $this->success($res['msg'], 'category/index');
                exit;
            } else {
                //执行失败
                $this->error($res['msg']);
                exit;
            }
        }
        $cate_id = input('param.cate_id');
        $data = $this->db->where('cate_id', $cate_id)->find();
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 编辑栏目
     */
    public function edit()
    {
        if (request()->isPost()) {
            $res = $this->db->edit(input('post.'));
            if ($res['valid']) {
                //执行成功
                $this->success($res['msg'], 'category/index');
                exit;
            } else {
                //执行失败
                $this->error($res['msg']);
                exit;
            }
        }
            //接受cate_id
            $cate_id = input('param.cate_id');
            //获取旧数据
            $oldData = $this->db->find($cate_id);
            $this->assign('oldData', $oldData);
            //处理所属分类【不能包含自己和自已的子集数据】
            $cateData = $this->db->getCateData($cate_id);
            $this->assign('cateData', $cateData);
            return $this->fetch();
    }

    public function del()
    {
        $res=$this->db->del(input('get.cate_id'));
        if($res['valid'])
        {
            //执行成功
            $this->success($res['msg'],'index');
            exit;
        }else{
            //执行失败
            $this->error($res['msg']);
            exit;
        }

    }
}
?>
